package org.dreamwork.modbus.test;

import org.dreamwork.modbus.tcp.IServerHandler;

import java.util.Arrays;

public class RandomHandler implements IServerHandler {
    @Override
    public boolean[] onReadCoils (int address, int count) {
        boolean[] result = new boolean[count];
        for (int i = 0; i < result.length; i ++) {
            result [i] = Math.random () > .5;
        }
        System.out.println (Arrays.toString (result));
        return result;
    }

    @Override
    public boolean[] onReadDiscreteInputs (int address, int count) {
        return onReadCoils (address, count);
    }

    @Override
    public int[] onReadHoldingRegisters (int address, int count) {
        int[] result = new int [count];
        for (int i = 0; i < result.length; i ++) {
            result[i] = (int) (Math.random () * 0xffff);
        }
        System.out.println (Arrays.toString (result));
        return result;
    }

    @Override
    public int[] onReadInputRegisters (int address, int count) {
        return onReadHoldingRegisters (address, count);
    }

    @Override
    public void onWriteSingleCoil (int address, boolean value) {
        System.out.println ("coil[" + address + "] set to " + value);
    }

    @Override
    public void onWriteSingleRegister (int address, int value) {
        System.out.println ("register[" + address + "] set to " + value);
    }

    @Override
    public void onWriteMultipleCoils (int address, boolean[] values) {
        System.out.println ("coil[" + address + '~' + (address + values.length - 1) + "] set to " + Arrays.toString (values));
    }

    @Override
    public void onWriteMultipleRegisters (int address, int[] values) {
        System.out.println ("register[" + address + '~' + (address + values.length - 1) + "] set to " + Arrays.toString (values));
    }
}